/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JListWithIconLabels.java
 *
 * Created on 11.06.2010, 18:09:46
 */

package de.jmda.mview;

import de.jmda.mview.typeshape.IconFactory;
import de.jmda.util.gui.awt.graphics.IconProvider;

import java.awt.Component;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;

import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 *
 *
 */
public class JListWithIconLabels extends javax.swing.JFrame {
	
	private class TypeElementListModel extends DefaultListModel
	{
		private TypeElement[] typeElements;

		private TypeElementListModel(Collection<TypeElement> elements)
		{
			this.typeElements = elements.toArray(new TypeElement[0]);

			Arrays.sort
			(
					this.typeElements,
					new Comparator<TypeElement>()
					{
						@Override
						public int compare(TypeElement o1, TypeElement o2)
						{
							return
									o1.getSimpleName().toString().compareTo(
									o2.getSimpleName().toString());
						}				
					}
			);
		}

		@Override
		public int getSize()
		{
			return typeElements.length;
		}

		@Override
		public Object getElementAt(int index)
		{
			return typeElements[index];
		}
	}

	private class CellRenderer extends JLabel implements ListCellRenderer
	{
		private CellRenderer()
		{
			setOpaque(true);
			setHorizontalAlignment(LEFT);
      setVerticalAlignment(CENTER);
		}

		@Override
		public Component getListCellRendererComponent(
				JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
		{
			if (isSelected)
			{
				setBackground(list.getSelectionBackground());
				setForeground(list.getSelectionForeground());
      }
			else
			{
				setBackground(list.getBackground());
				setForeground(list.getForeground());
      }

			if (value instanceof Element)
			{
				setIcon(IconFactory.getIcon((Element) value));
			}
			setText(value.toString());

			return this;
		}
	}

    /** Creates new form JListWithIconLabels */
    public JListWithIconLabels() {
        initComponents();

				Collection<TypeElement> typeElements =
						ElementFactory.getTypeElements().values();

				jList1.setModel(new TypeElementListModel(typeElements));
				jList1.setCellRenderer(new CellRenderer());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jList1 = new javax.swing.JList();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jList1.setModel(new javax.swing.AbstractListModel() {
      String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
      public int getSize() { return strings.length; }
      public Object getElementAt(int i) { return strings[i]; }
    });
    jScrollPane1.setViewportView(jList1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(137, 137, 137)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(136, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(73, 73, 73)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(97, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JListWithIconLabels().setVisible(true);
            }
        });
    }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JList jList1;
  private javax.swing.JScrollPane jScrollPane1;
  // End of variables declaration//GEN-END:variables

}